using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;


namespace Marcelo
{
	// http://blogs.msdn.com/marcelolr/archive/2006/03/03/543301.aspx
	internal class DropPreviewAdorner : Adorner
	{

		public DropPreviewAdorner(UIElement adornedElement, UIElement adorningElement)
			: base(adornedElement)
		{
			VisualBrush brush = new VisualBrush(adorningElement);
			m_Child = new Rectangle();
			m_Child.Width = adorningElement.RenderSize.Width;
			m_Child.Height = adorningElement.RenderSize.Height;
			m_Child.Fill = brush;
			m_Child.IsHitTestVisible = false;

			brush.Opacity = 0.7;
			
			this.Width = m_Child.Width;
			this.Height = m_Child.Height;

		}
		private Rectangle m_Child;
		private double m_LeftOffset;
		private double m_TopOffset;

		protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint)
		{
			m_Child.Measure(constraint);
			return m_Child.DesiredSize; // new Size(m_Child.Width, m_Child.Height);// 
		}

		protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize)
		{
			m_Child.Arrange(new Rect(finalSize));
			return finalSize;
		}

		protected override System.Windows.Media.Visual GetVisualChild(int index)
		{
			return m_Child;
		}

		protected override int VisualChildrenCount
		{
			get
			{
				return 1;
			}
		}

		public double LeftOffset
		{
			get
			{
				return m_LeftOffset;
			}
			set
			{
				m_LeftOffset = value;
				UpdatePosition();
			}
		}

		public double TopOffset
		{
			get
			{
				return m_TopOffset;
			}
			set
			{
				m_TopOffset = value;
				UpdatePosition();
			}
		}

		private void UpdatePosition()
		{
			AdornerLayer adornerLayer = this.Parent as AdornerLayer;
			if (adornerLayer != null)
			{
				adornerLayer.Update(AdornedElement);
			}
		}

		public override System.Windows.Media.GeneralTransform GetDesiredTransform(System.Windows.Media.GeneralTransform transform)
		{
			GeneralTransformGroup result = new GeneralTransformGroup();
			result.Children.Add(base.GetDesiredTransform(transform));
			result.Children.Add(new TranslateTransform(LeftOffset, TopOffset));
			return result;
		}
	}
}
